/* 		ALICE v0.0.0.2
 * 		Notes:
 * Got the bloody getline working, now processing
 * should be done by dynamically creating a database
 * and adding new catagories with it. Will have to look
 * up how to do that. 
 * Got it to recognise if a word is in a file!
*/

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include "Processing.h"

using namespace std;

void CheckLoadFunc(string strFileName)
{
	ifstream LoadFile;
	/* Checking if a file exists */
	strFileName += ".bot";
	LoadFile.open(strFileName.c_str()); /* Attempts to open the file */
	cout << "Loading: " << strFileName << "\n";
	if (!LoadFile) /* If the file cannot be opened */
		{ 
			LoadFile.clear(ios::failbit);
			cout << "\"" << strFileName << "\" Does not exist... Creating it now.\n";
			LoadFile.close();
			ofstream CreateLoadFile (strFileName.c_str()); /* Creates the file */
			if (!CreateLoadFile)
			{
				cout << "Unknown Error when creating the file\n";
			}
			else
			{
				CreateLoadFile << "";
				CreateLoadFile.close();
			}
			ifstream LoadFile;
			LoadFile.open(strFileName.c_str()); /* Reopens the file to check it was created */
			if (!LoadFile)
			{
				cout << "Unknown Error when checking if the file was created\n";
			}
			else /* Has completed */
			{
				cout << "Done!\n";
			}
			LoadFile.close();
		}
	/* File checking ends here */
}

void FileChecks()
{
	CheckLoadFunc("nouns");
	CheckLoadFunc("verbs");
	CheckLoadFunc("adverbs");
	CheckLoadFunc("adjectives");
	cout << "\nReady!!\n------------------\n";
}

void CommandProccess(string strMainInput) /* Processing for scripted commands */
{
	/* Add the word add function here */
	cout << "Command taken\n"; /* Debug Code */
}

int GetNumWords(string strSentence) /* Gets the number of words in a sentence */
{
	int intNumWords = 0;
	if (int(strSentence.length()) > 0) /* Stops it saying there is a word */	
	{								   /* there if you don't input anything */
		intNumWords++;
	}	
	for (int i = 0;i <= int(strSentence.length());i++)
	{
		if (strSentence[i] == ' '){intNumWords++;} /*Counts the number of spaces in the sentence */
	}											   /* this will keep any punctuation to later process */ 
	return intNumWords;
}
void ProcessFunc(string* strWords,int intNumOfWords) /*It works bitches!*/
{
		prc::processfunc(strWords,intNumOfWords);
}
void StringSplit(string strInput) /*This function splits a string into an array */
{
	if (strInput[0] == '#')
	{
		CommandProccess(strInput);
	}
	else
	{
		int intNumOfWords = GetNumWords(strInput);
		string strWords[intNumOfWords];
		istringstream isInput(strInput);
		for (int i=0;i<intNumOfWords;i++)
		{
			getline(isInput,strWords[i],' ');
			for (unsigned int j=0; j<strWords[i].length(); j++)
			{
				strWords[i][j]=tolower(strWords[i][j]);
			}
		}
		ProcessFunc(strWords,intNumOfWords);
	}
}

void MainLoop()
{
	string strMainInput;
	while ((strMainInput != "#Quit") || (strMainInput != "#quit"))
	{
		getline(cin,strMainInput);
		StringSplit(strMainInput);
	}
}

int main()
{
	cout << " ----------------\n" << "| ALICE v0.0.0.2 |\n" << " ----------------\n";
	FileChecks();
	MainLoop();
	return 0;
}
